import 'package:dio/dio.dart';

import 'http_connection_check.dart';
import 'http_export.dart';

String? httpErrorMessage(HttpResponse response,
    {String defaultError = "error"}) {
  return HttpHandleError.customErrorMessage(response,
      defaultError: defaultError);
}

abstract class HttpHandleError {
  static HttpResponse adjust(HttpResponse response, {DioException? exception}) {
    if (response.type == DioExceptionType.badResponse) {
      response.message = "服务器错误";
    } else if (response.type == DioExceptionType.connectionError) {
      response.message = isNoNetWork ? "网络异常" : "服务器错误";
    } else if (response.type == DioExceptionType.connectionTimeout) {
      response.message = "连接超时";
    } else if (response.type == DioExceptionType.sendTimeout) {
      response.message = "请求超时";
    } else if (response.type == DioExceptionType.receiveTimeout) {
      response.message = "响应超时";
    } else if (exception?.error is FormatException) {
      response.message = "服务器错误";
    } else if (response.code != null) {
      if (response.code! > 500 && response.code! < 600) {
        response.message = "服务器错误";
      } else {
        response.message = customErrorMessage(response);
      }
    }
    return response;
  }

  static String? customErrorMessage(HttpResponse response,
      {String defaultError = "服务器错误"}) {
    // if (response.code != null &&
    //     response.code! > 33000 &&
    //     response.code! < 100000) {
    //   switch (response.code) {
    //     default:
    //       return defaultError;
    //   }
    // }

    return (response.message ?? '').isNotEmpty
        ? response.message
        : defaultError;
  }
}
